package com.noble.meether.ui.dialog;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.noble.meether.R;
import com.noble.library.view.base.BaseDialogFragment;

/**
 * 全局统一消息提示弹框
 */
public class MsgTipDialog extends BaseDialogFragment implements View.OnClickListener {
    private TextView tv_content, btn_cancel;

    private String content;
    private OnDialogClickListener dialogClickListener;

    private boolean btnSingle = false;   // 单按钮

    public MsgTipDialog() {
    }

    @SuppressLint("ValidFragment")
    public MsgTipDialog(String content, OnDialogClickListener dialogClickListener) {
        initDialog();

        this.content = content;
        this.dialogClickListener = dialogClickListener;
    }

    private void initDialog() {
        settWindowAnimations(R.style.AnimDownInDownOutOverShoot);
        setGravity(Gravity.CENTER);
        setDialogSizeRatio(0, 0);
        setCanceledOnTouchOutside(false);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        setContentView(R.layout.msg_tip_dialog);
        initView();
        return getContentView();
    }

    private void initView() {
        tv_content = (TextView) findViewById(R.id.tv_content);
        findViewById(R.id.btn_add).setOnClickListener(this);
        btn_cancel = (TextView) findViewById(R.id.btn_cancel);
        btn_cancel.setOnClickListener(this);
        findViewById(R.id.btn_close).setOnClickListener(this);

        tv_content.setText(content);

        if (btnSingle) btn_cancel.setVisibility(View.GONE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_add:
                if (dialogClickListener != null) {
                    dialogClickListener.onSubmit();
                }
                dismiss();
                break;
            case R.id.btn_cancel:
                if (dialogClickListener != null) {
                    dialogClickListener.onCancel();
                }
                dismiss();
                break;
            case R.id.btn_close:
                dismiss();
                break;
        }
    }

    public void isSingle(boolean btnSingle) {
        this.btnSingle = btnSingle;
    }


    public interface OnDialogClickListener {
        void onSubmit();

        void onCancel();
    }
}

